package com.example.course_topic.controller;

import com.example.course_topic.service.TopicService;
import com.example.model.common.R;
import com.example.model.pojo.CourseTopic;
import com.example.model.pojo.User;
import com.example.model.pojo.dto.TopicDTO;
import com.example.model.pojo.vo.TopicVO;

import com.example.model.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 题目控制器
 */
@RestController
@RequestMapping("/topics")
public class TopicController {

    @Autowired
    private TopicService topicService;


    //   获取题目列表    /topics   GET
    @GetMapping
    public R<Map<String, Object>> getTopicList(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size,
            @RequestParam(required = false, defaultValue = "1") Integer active) {
        try {

            Map<String, Object> result = topicService.getTopicList(page, size, active);
            return R.success(result);

        } catch (Exception e) {
            return R.fail(404,"获取题目列表失败");
        }
    }


    // 获取题目详情   /topics/{id}   GET
    @GetMapping("/{id}")
    public R<TopicVO> getTopicById(@PathVariable Integer id) {
        try {
            TopicVO topicVO = topicService.getTopicById(id);
            if (topicVO == null) {
                return R.fail(404, "题目不存在");
            }
            return R.success(topicVO);
        } catch (Exception e) {
            return R.fail(500, "获取题目详情失败");
        }
    }

    //  创建题目（教师）   /topics   POST
    @PostMapping
    public R<Void> createTopic(@RequestBody TopicDTO topicDTO) {
        try {
            User currentUser = UserHolder.get();
            if (currentUser == null) {
                return R.fail(401, "用户未登录");
            }
            Integer teacherId = currentUser.getId();
            String teacherName = currentUser.getName();

            // 参数校验
            if (topicDTO.getTitle() == null || topicDTO.getTitle().trim().isEmpty()) {
                return R.fail(400, "题目标题不能为空");
            }
            CourseTopic topic = new CourseTopic();
            topic.setTitle(topicDTO.getTitle());
            topic.setDescription(topicDTO.getDescription());
            topic.setMaxStudents(topicDTO.getMaxStudents());
            topic.setIsActive(topicDTO.getIsActive());
            topic.setTeacherId(teacherId);

            // topic对象不设置id属性,该属性自动增长,不影响创建
            boolean result = topicService.createTopic(topic);
            if (result) {
                return R.success() ;
            } else {
                throw new Exception("失败");
            }
        } catch (Exception e) {
            return R.fail(500, "创建题目失败");
        }
    }

    /**
     * 更新题目（教师）
     */
    @PutMapping("/{id}")
    public R<Void> updateTopic(@PathVariable Integer id, @RequestBody TopicDTO topicDTO) {
        try {
            User currentUser = UserHolder.get();
            if (currentUser == null) {
                return R.fail(401, "用户未登录");
            }
            Integer teacherId = currentUser.getId();


            boolean result = topicService.updateTopic(id, topicDTO);
            if (result) {
                System.out.println("result: " + result);
                return R.success() ;
            } else {
                throw new Exception("失败");
            }
        } catch (Exception e) {
            return R.fail(500, "更新题目失败");
        }
    }

    /**
     * 删除题目（教师）
     */
    @DeleteMapping("/{id}")
    public R<Void> deleteTopic(@PathVariable Integer id) {
        try {
            System.out.println("尝试删除");
            boolean success = topicService.deleteTopic(id);
            if (!success) {
                return R.fail(500, "删除题目失败");
            }
            return R.success() ;
        } catch (Exception e) {
            return R.fail(500, "删除题目失败");
        }
    }
}